package com.woniuxy.web;

import com.woniuxy.pojo.GuangChang;
import com.woniuxy.pojo.Person3;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;

@Controller
@Validated
@RequestMapping("/aaa2")
public class MaFanController {

    @RequestMapping("/bbb")
    public ModelAndView updateUser(@Valid Person3 p, BindingResult bindingResult){
        ModelAndView modelAndView = new ModelAndView();
        System.out.println(p);
        if (bindingResult.hasErrors()) {
            System.out.println(1);
            FieldError fieldError = bindingResult.getFieldError();
            modelAndView.addObject("bindingResult", fieldError.getField() + " ：" + bindingResult.getFieldError().getDefaultMessage());
        }else{
                modelAndView.addObject("p", "传入的对象是："+p);
        }

        modelAndView.setViewName("vvv");
        return modelAndView;
    }
        @RequestMapping("/ccc")
        //@RequestBody 这个注解可以接收前端传的Json对象，把json字符串转换成对象 sb
        public ModelAndView addClazzRoom(@Valid GuangChang g){
            ModelAndView modelAndView = new ModelAndView();
            System.out.println(g);
            modelAndView.addObject("ggg", "传入的Map集合是："+g);
            modelAndView.setViewName("vvv");
            return modelAndView;
        }
    @RequestMapping("/forword2")
//这个返回值类型String，可以自动帮我跳转到对应的jsp页面。
    public String doAddClazzRoom() {
        System.out.println("这里的执行forword2我们的业务代码……");
        //执行转发
        return "forward:/";
    }
    @RequestMapping("/login")
    public String login(){
        //这个东西就会自动通过视图解析器，拼接前缀后缀 /WEB-INF/jsp/login.jsp页面
        return "redirect:index.jsp";
    }
}

